import os
from torchvision import transforms, datasets
from torch.utils.data import DataLoader

# 数据集根路径（根据实际路径修改）
master_folder = r"C:\Users\lenovo\.cache\kagglehub\datasets\anshtanwar\pets-facial-expression-dataset\versions\11\Master Folder"

# 数据增强（训练集）
train_transform = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# 验证集（无增强）
val_transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# 加载数据集（绝对路径）
train_dataset = datasets.ImageFolder(root=os.path.join(master_folder, "train"), transform=train_transform)
val_dataset = datasets.ImageFolder(root=os.path.join(master_folder, "valid"), transform=val_transform)
test_dataset = datasets.ImageFolder(root=os.path.join(master_folder, "test"), transform=val_transform)

# 检查类别
print("训练集类别：", train_dataset.classes)

# 定义数据加载器
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False, num_workers=4)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4)